package com.koron.sinoprobe.web.engineering.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.po.SpDivision;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.po.SpSettlementAudit;
import com.koron.sinoprobe.web.engineering.bean.query.SpSettlementAuditQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpSettlementAuditVO;
import com.koron.sinoprobe.web.engineering.mapper.SpSettlementAuditMapper;
import com.koron.sinoprobe.web.engineering.service.SpDivisionService;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.engineering.service.SpSettlementAuditService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectVO;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 结算审查(SpSettlementAudit)表服务实现类
 *
 * @author jzp
 * @since 2021-12-02 10:00:02
 */
@Service
public class SpSettlementAuditServiceImpl implements SpSettlementAuditService {

    @Autowired
    private SpEstablishService spEstablishService;

    @Autowired
    private SpProjectService spProjectService;

    @Autowired
    private SpDivisionService spDivisionService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpSettlementAuditVO getById(SessionFactory factory, String id) {
        SpSettlementAuditMapper mapper = factory.getMapper(SpSettlementAuditMapper.class);
        SpSettlementAuditVO spSettlementAuditVO = mapper.selectById(id);
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spSettlementAuditVO.getProjectId());
        spSettlementAuditVO.setSpEstablish(spEstablish);
        SpProject spProject = spProjectService.getById(factory, spSettlementAuditVO.getProjectId());
        spSettlementAuditVO.setSpProject(spProject);
        SpDivision spDivision = spDivisionService.getByProjectId(factory, spSettlementAuditVO.getProjectId());
        spSettlementAuditVO.setSpDivision(spDivision);
        return spSettlementAuditVO;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpSettlementAuditVO> list(SessionFactory factory, SpSettlementAuditQuery query) {
        SpSettlementAuditMapper mapper = factory.getMapper(SpSettlementAuditMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpSettlementAuditVO> page(SessionFactory factory, PageQuery pageQuery, SpSettlementAuditQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpSettlementAuditVO> spSettlementAuditList = list(factory, query);
        return new PageInfo<>(spSettlementAuditList);
    }

    @Override
    @TaskAnnotation("getRelated")
    public List<String> getRelated(SessionFactory factory) {
        SpSettlementAuditMapper mapper = factory.getMapper(SpSettlementAuditMapper.class);
        return mapper.selectRelated();
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpSettlementAudit spSettlementAudit) {
        // 默认草稿
        if (StringUtils.isEmpty(spSettlementAudit.getStatus())) {
            spSettlementAudit.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpSettlementAuditMapper mapper = factory.getMapper(SpSettlementAuditMapper.class);
        return mapper.insertOrUpdate(spSettlementAudit) > 0;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        // 1. 更新状态
        SpSettlementAudit spSettlementAudit = new SpSettlementAudit();
        spSettlementAudit.setId(id);
        spSettlementAudit.setStatus(Constants.AppceptStatus.APPROVED);
        spSettlementAudit.setFinishTime(new Date());
        update(factory, spSettlementAudit);

        // 2023-08-17 结算审查完成后项目状态改为100
        SpSettlementAuditVO settlementAuditVO = getById(factory, id);
        SpProjectVO spProjectVO = spProjectService.selectOne(factory, settlementAuditVO.getProjectId());
        spProjectVO.setProgress(100);
        spProjectService.saveOrUpdate(factory, spProjectVO);
        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpSettlementAudit spSettlementAudit) {
        SpSettlementAuditMapper mapper = factory.getMapper(SpSettlementAuditMapper.class);
        return mapper.update(spSettlementAudit) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpSettlementAudit spSettlementAudit = new SpSettlementAudit();
        spSettlementAudit.setId(id);
        spSettlementAudit.setStatus(status);
        return update(factory, spSettlementAudit);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpSettlementAudit source = getById(factory, id);
        Assert.state(source.getStatus().equals(Constants.AppceptStatus.DRAFT), "非草稿状态不允许删除");
        SpSettlementAuditMapper mapper = factory.getMapper(SpSettlementAuditMapper.class);
        return mapper.hidden(id) > 0;
    }
}
